// pages/index/free/quote.js
var app = getApp();
var call = require("../../../utils/request.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    clicked: 2,
    clickede: 1,
    clickedes: 1,
    clickedese: 2,
    chooseradio: '0',
    afferent: '',
    mobile: '',
    area: '',
    radvalue: 1,
    radname: '',
    main_room: "3室",
    main_tin: "2厅",
    main_wei: "2卫",
    main_yang: "3阳台",
    houstyle: '3223',
    room: 1,
    tin: 1,
    wei: 1,
    yan: 1,
    city: '',
    cityid: '',
    proviceid: '',
    sid: '',
    land_page: '',
    colist: [{
        name: '人工费',
        price: '?'
      },
      {
        name: '材料费',
        price: '?'
      },
      {
        name: '设计费',
        price: '?'
      },
      {
        name: '质检费',
        price: '?'
      }
    ],

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    //获取secction信息
    call.getData('session', this.shuffleSuc, this.fail);
    call.getOrigin('origin', this.origisucess, this.originfail);
  },
  //获取session
  shuffleSuc: function(res) {
    console.log(res)
    var that = this;
    var sid = res.data.PHPSESSID
    wx.setStorage({ //存储到本地
      key: "sid",
      data: sid
    })
    that.setData({
      sid: sid
    })

  },
  fail: function() {
    console.log("失败")
  },
  //获取页面来源
  origisucess: function(res) {
    console.log(res)
    var that = this;
    console.log('请求成功', res)
    var land_page = res.datas.landing_page
    var housetype = res.datas.house_type
    var decorate = res.datas.decorate_type
    that.setData({
      land_page: land_page,
      housetype: housetype,
      decore: decorate
    })

  },
  originfail: function() {
    console.log("失败")
  },
  choosecity: function(e) {
    wx.navigateTo({
      url: '../public/custom/cucity',
    })

  },

  //显示蒙版
  chooroom: function() {
    var that = this;
    this.setData({
      showModal: true,
      Modalcontent: true
    })
  },
  //弹框隐藏
  confirm: function() {
    var that = this;
    this.setData({
      showModal: false,
      Modalcontent: false

    })
  },
  bindmask: function () {
    this.setData({
      showinfo: false,
      showModal: false,
      Modalcontent: false
    })
  },
  hidemask: function(e) {
    this.setData({
      showModal: false,
      Modalcontent: false,
      showinfo: false
    })
    console.log(1232)
  },
  cancle: function() {
    var that = this;
    this.setData({
      showModal: false,
      Modalcontent: false
    })
  },
  //房屋类型弹框内容点击选择
  choose_btn: function(e) {
    var that = this;
    var room = e.target.dataset.room
    var tin = that.data.tin
    var wei = that.data.wei
    var yan = that.data.yan
    var housty = +room + '' + tin + '' + wei + '' + yan + ''

    var index = parseInt(e.currentTarget.id);
    var value = that.data.housetype[index].room;
    if (this.data.clicked == e.currentTarget.id) {
      this.setData({
        clickId: +1,
      })
      return;
    }
    that.setData({
      clicked: e.currentTarget.id,
      main_room: value,
      room: room,
      houstyle: housty
    })
    console.log(e)
  },

  choose_tin: function(e) {
    var that = this;
    var index = parseInt(e.currentTarget.id);
    var value = that.data.housetype[index].hall;
    var room = that.data.room
    var tin = e.target.dataset.tin
    var wei = that.data.wei
    var yan = that.data.yan
    var housty = +room + '' + tin + '' + wei + '' + yan + ''
    if (this.data.clickede == e.currentTarget.id) {
      this.setData({
        clickIde: +1,
      })
      return;
    }
    that.setData({
      clickede: e.currentTarget.id,
      main_tin: value,
      tin: tin,
      houstyle: housty
    })
    console.log(value)
  },

  choose_wei: function(e) {
    var that = this;
    var index = parseInt(e.currentTarget.id);
    var value = that.data.housetype[index].toilet;
    var room = that.data.room
    var tin = that.data.tin
    var wei = e.target.dataset.wei
    var yan = that.data.yan
    var housty = +room + '' + tin + '' + wei + '' + yan + ''
    if (this.data.clickedes == e.currentTarget.id) {
      this.setData({
        clickIdes: +1,
      })
      return;
    }
    that.setData({
      clickedes: e.currentTarget.id,
      main_wei: value,
      wei: wei,
      houstyle: housty
    })
    console.log(value)
  },

  choose_sun: function(e) {
    var that = this;
    var index = parseInt(e.currentTarget.id);
    var value = that.data.housetype[index].balcony;
    var room = that.data.room
    var tin = that.data.tin
    var wei = that.data.wei
    var yan = e.target.dataset.tty
    var housty = +room + '' + tin + '' + wei + '' + yan + ''

    if (this.data.clickedese == e.currentTarget.id) {
      this.setData({
        clickIdese: +1,
      })
      return;
    }
    that.setData({
      clickedese: e.currentTarget.id,
      main_yang: value,
      yan: yan,
      houstyle: housty
    })
    console.log(value)
  },
  //面积，装修方式，手机号码获取
  radioChange: function(e) {
    var radvalue = this.data.radvalue
    var values = e.currentTarget.dataset.id
    var name = e.currentTarget.dataset.name
    if (this.data.clickedes == e.currentTarget.id) {
      this.setData({
        chooseradio: +1,
      })
      return;
    }
    this.setData({
      radvalue: values,
      chooseradio: e.currentTarget.id,
    })
    console.log(values)
  },
  givearea: function(e) {
    var value = e.detail.value
    this.setData({
      area: value
    })
  },
  givetele: function(e) {
    var value = e.detail.value
    this.setData({
      mobile: value
    })
  },
  settime: function () {
    var that = this
    setTimeout(function () {
      that.setData({
        alertmess: false
      })
    }, 2000)
  },
  //跳转到报价结果页面
  skipsrsult: function(e) {
    var that = this
    var arr = []
    var alea = that.data.area
    var mobile = that.data.mobile
    var radios = that.data.radvalue
    var mainroom = that.data.main_room
    var ting = that.data.main_tin
    var wei = that.data.main_wei
    var yang = that.data.main_yang
    var houstyle = that.data.houstyle
    var cityid = that.data.cityid
    var proviceid = that.data.proviceid
    var city = that.data.city
    var sid = that.data.sid
    var land_page = that.data.land_page
    console.log('这个是', sid)
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    console.log(mobile)
    if (city == '') {
      that.setData({
        alertmess: true,
        message: '请选择城市！'
      })
      that.settime()
    } else if (alea == '') {
      that.setData({
        alertmess: true,
        message: '请填写房屋面积！'
      })
      that.settime()
    } else if (radios == '') {
      that.setData({
        alertmess: true,
        message: '装修类型未选择！'
      })
      that.settime()
    } else if (mobile == '') {
      that.setData({
        alertmess: true,
        message: '请填写手机号码！'
      })   
      that.settime()
    } else if (mobile.length != 11) {
      that.setData({
        alertmess: true,
        message: '请重新输入手机号码！'
      })
      that.settime()
    } else if (!myreg.test(mobile)) {
      that.setData({
        alertmess: true,
        message: '手机号码有误,请重新输入！'
      })
      that.settime()
    } else {
      // this.getevaluate()
      wx.request({
        url: app.apiUrl("c=zhaobiao/index&a=submit_estimate"),
        data: {
          phone: mobile,
          acreage: alea,
          house_type: houstyle,
          decorate_type: radios,
          province: proviceid,
          city: cityid,  
          landing_page: land_page
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Cookie': 'PHPSESSID=' + sid
        },
        method: 'POST',
        success: function(res) {
          var id = 1
          console.log('新的值为', res.data)
          var code = res.data.code
          var error = res.data.error
          if (code == 200) {
            var bid_id = res.data.datas.bid_id
            var unid = res.data.datas.uuid
            wx.navigateTo({
              url: 'quote_result?data=' + [id, alea, radios, mainroom, ting, wei, yang, city, mobile, bid_id, unid]
            })
          } else if (code == 400) {
            that.setData({
              alertmess: true,
              message: error
            })
            that.settime()
          }
        },
        fail: function(res) {
          console.log('请求失败', res.data)
        }
      })
    }
    console.log(arr)
  },

  //阻止默认事件
  stopmove: function(e) {

  },

})